package com.tj.collection.controller.lending.response;

import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.EntityUtils;
import com.tj.collection.db.entity.ClCommentEntity;
import com.tj.collection.db.entity.ClPhoneRecordEntity;
import com.tj.collection.db.entity.ClReceiverDetailEntity;
import com.tj.collection.enums.ClUserContactTypeEnum;
import com.tj.collection.enums.UserContactFamilyTypeEnum;
import com.tj.collection.enums.UserContactFriendTypeEnum;
import com.tj.collection.utils.IndiaMobileUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2020/01/10
 * Time: 下午2:45
 */
public class ClEmergencyContactResponse implements ResponseData {

    /**
     * 亲属/社会关系联系人列表
     */
    private List<Item> contacts = new ArrayList<>();

    /**
     * 直系亲属联系人类型选项
     */
    private List<String> familyContactTypeOptions = UserContactFamilyTypeEnum.getDescList();

    /**
     * 同事朋友联系人类型选项
     */
    private List<String> friendContactTypeOptions = UserContactFriendTypeEnum.getDescList();

    public ClEmergencyContactResponse() {
    }

    public ClEmergencyContactResponse(ClReceiverDetailEntity clReceiverDetailEntity, List<ClCommentEntity> clCommentEntityList, List<ClPhoneRecordEntity> clPhoneRecordEntityList) {
        Map<String, ClCommentEntity> clCommentEntityMap = EntityUtils.makeEntityMap(clCommentEntityList, "mobile");
        Map<String, ClPhoneRecordEntity> clPhoneRecordEntityMap = EntityUtils.makeEntityMap(clPhoneRecordEntityList, "mobile");

        //本人
        Item emergentContact = new Item();
        emergentContact.setName(clReceiverDetailEntity.getName());
        emergentContact.setMobile(IndiaMobileUtil.countryCodeFilter(clReceiverDetailEntity.getMobile()));

        ContentData contentData = convertContentData(emergentContact.getMobile(), clCommentEntityMap, clPhoneRecordEntityMap);
        if (contentData != null) {
            emergentContact.setCollectionContactRecord(contentData.getContent());
            emergentContact.setLastCallTime(contentData.getDate());
            emergentContact.setLastCallDescribe(contentData.getIntervalDays() + " days ago");
        }
        emergentContact.setRelation("Self");
        contacts.add(emergentContact);

        //家庭紧急联系人
        Item emergencyContactVO = new Item();
        ClUserContactTypeEnum clUserContactTypeEnum1 = ClUserContactTypeEnum.getEnum(clReceiverDetailEntity.getContact1Relative());
        emergencyContactVO.setRelation(clUserContactTypeEnum1 == null ? "" : clUserContactTypeEnum1.getDesc())
                .setMobile(IndiaMobileUtil.countryCodeFilter(clReceiverDetailEntity.getContact1Mobile()))
                .setName(clReceiverDetailEntity.getContact1Name());
        ContentData familyContentData = convertContentData(emergencyContactVO.getMobile(), clCommentEntityMap, clPhoneRecordEntityMap);
        if (familyContentData != null) {
            emergencyContactVO.setCollectionContactRecord(familyContentData.getContent());
            emergencyContactVO.setLastCallTime(familyContentData.getDate());
            emergencyContactVO.setLastCallDescribe(familyContentData.getIntervalDays() + " days ago");
        }
        contacts.add(emergencyContactVO);
        //朋友紧急联系人
        Item emergency = new Item();
        ClUserContactTypeEnum clUserContactTypeEnum2 = ClUserContactTypeEnum.getEnum(clReceiverDetailEntity.getContact2Relative());
        emergency.setRelation(clUserContactTypeEnum2 == null ? "" : clUserContactTypeEnum2.getDesc())
                .setMobile(IndiaMobileUtil.countryCodeFilter(clReceiverDetailEntity.getContact2Mobile()))
                .setName(clReceiverDetailEntity.getContact2Name());
        ContentData friendContentData = convertContentData(emergency.getMobile(), clCommentEntityMap, clPhoneRecordEntityMap);
        if (friendContentData != null) {
            emergency.setCollectionContactRecord(friendContentData.getContent());
            emergency.setLastCallTime(friendContentData.getDate());
            emergency.setLastCallDescribe(friendContentData.getIntervalDays() + " days ago");
        }
        contacts.add(emergency);
    }

    private ContentData convertContentData(String mobile, Map<String, ClCommentEntity> clCommentEntityMap, Map<String, ClPhoneRecordEntity> clPhoneRecordEntityMap) {
        ClCommentEntity clCommentEntity = clCommentEntityMap.get(mobile);
        ClPhoneRecordEntity clPhoneRecordEntity = clPhoneRecordEntityMap.get(mobile);
        if (clCommentEntity == null && clPhoneRecordEntity == null) {
            return null;
        }
        if (clCommentEntity != null && clPhoneRecordEntity == null) {
            return new ContentData()
                    .setContent(clCommentEntity.getContent())
                    .setDate(DateUtil.getDate(clCommentEntity.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"))
                    .setIntervalDays(DateUtil.diffDate(new Date(), clCommentEntity.getGmtCreate()));
        }
        if (clCommentEntity == null && clPhoneRecordEntity != null) {
            return new ContentData()
                    .setContent(clPhoneRecordEntity.getContent())
                    .setDate(DateUtil.getDate(clPhoneRecordEntity.getGmtCreate()))
                    .setIntervalDays(DateUtil.diffDate(new Date(), clPhoneRecordEntity.getGmtCreate()));
        }
        if (clCommentEntity != null && clPhoneRecordEntity != null) {
            if (clCommentEntity.getGmtCreate().getTime() >= clPhoneRecordEntity.getGmtCreate().getTime()) {
                return new ContentData()
                        .setContent(clCommentEntity.getContent())
                        .setDate(DateUtil.getDate(clCommentEntity.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"))
                        .setIntervalDays(DateUtil.diffDate(new Date(), clCommentEntity.getGmtCreate()));
            } else {
                return new ContentData()
                        .setContent(clPhoneRecordEntity.getContent())
                        .setDate(DateUtil.getDate(clPhoneRecordEntity.getGmtCreate()))
                        .setIntervalDays(DateUtil.diffDate(new Date(), clPhoneRecordEntity.getGmtCreate()));
            }
        }
        return null;
    }


    public List<Item> getContacts() {
        return contacts;
    }

    public void setContacts(List<Item> contacts) {
        this.contacts = contacts;
    }

    public List<String> getFamilyContactTypeOptions() {
        return familyContactTypeOptions;
    }

    public List<String> getFriendContactTypeOptions() {
        return friendContactTypeOptions;
    }

    private static class Item {
        /**
         * 与借款人关系
         */
        private String relation;
        /**
         * 手机号码
         */
        private String mobile;
        /**
         * 通讯录姓名
         */
        private String name;
        /**
         * 最近一次联系时间
         */
        private String lastCallTime;
        /**
         * 最近一次联系时间 文字描述
         */
        private String lastCallDescribe;
        /**
         * 催收备注
         */
        private String collectionContactRecord;

        public String getRelation() {
            return relation;
        }

        public Item setRelation(String relation) {
            this.relation = relation;
            return this;
        }

        public String getMobile() {
            return mobile;
        }

        public Item setMobile(String mobile) {
            this.mobile = mobile;
            return this;
        }

        public String getName() {
            return name;
        }

        public Item setName(String name) {
            this.name = name;
            return this;
        }

        public String getLastCallTime() {
            return lastCallTime;
        }

        public Item setLastCallTime(String lastCallTime) {
            this.lastCallTime = lastCallTime;
            return this;
        }

        public String getLastCallDescribe() {
            return lastCallDescribe;
        }

        public Item setLastCallDescribe(String lastCallDescribe) {
            this.lastCallDescribe = lastCallDescribe;
            return this;
        }

        public String getCollectionContactRecord() {
            return collectionContactRecord;
        }

        public Item setCollectionContactRecord(String collectionContactRecord) {
            this.collectionContactRecord = collectionContactRecord;
            return this;
        }
    }

    private static class ContentData {
        private String content;
        private String date;
        private Integer intervalDays;

        public String getContent() {
            return content;
        }

        public ContentData setContent(String content) {
            this.content = content;
            return this;
        }

        public String getDate() {
            return date;
        }

        public ContentData setDate(String date) {
            this.date = date;
            return this;
        }

        public Integer getIntervalDays() {
            return intervalDays;
        }

        public ContentData setIntervalDays(Integer intervalDays) {
            this.intervalDays = intervalDays;
            return this;
        }
    }


}
